﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Medival_Snake
{
    class Patrol
    {
        Objects NPC;
        Vector2 TempLoc;

        PathFinder pathFinder;

        GameTime gameTime;

        public Patrol(Objects npc)
        {
            this.NPC = npc;
            TempLoc = new Vector2(NPC.Sensor.X, NPC.Sensor.Y);
            pathFinder = new PathFinder(NPC);
        }

        bool PatrolingLeft = false;
        bool PatrolingRight = false;

        bool PatrolingUp = false;
        bool PatrolingDown = false;

        public static bool Alarm = false;

        public static bool chase = false;

        float time = 0;
        public static float ChaseTimer = 0;

        int l1;
        int r1;

        public bool SensorCheck = true;
        

        public void PatrolX(int x1, int x2,GameTime gameTime) //x patrol
        {

            this.gameTime = gameTime;

            if (PatrolingLeft)//patrol left
            {
                TempLoc.X += -NPC.Move.X;
                NPC.SightLeft = true;
                NPC.SightRight = false;
            }

            if (PatrolingRight)//patrol right
            {
                TempLoc.X += NPC.Move.X;
                NPC.SightRight = true;
                NPC.SightLeft = false;
            }

            if (TempLoc.X <= x1 || PatrolingLeft && SensorCheck == false)             //end point left
            {   
                    PatrolingLeft = false;
                    PatrolingRight = true;

                    if (l1 == 0)
                    {
                        time = 1;
                    }
                    l1 = 1;
                    r1 = 0;
            }

            if (TempLoc.X >= x2 || PatrolingRight && SensorCheck == false)            // end point right
            {

                    PatrolingLeft = true;
                    PatrolingRight = false;

                    if (r1 == 0)
                    {
                        time = 1;
                    }
                    r1 = 1;
                    l1 = 0;
            }

            NPC.Sensor.X = (int)TempLoc.X;
            NPC.Sensor.Y = (int)TempLoc.Y;
            
            for (int i = 0; i < Game1.Lvl1.Solids.Count; i++) //check for base map collision
            {
                if (Game1.Lvl1.Solids[i].Intersects(NPC.Sensor))
                {
                    SensorCheck = false;
                    break;
                }
            }

            if (SensorCheck)
            {
                NPC.Position.X = TempLoc.X;
                NPC.Position.Y = TempLoc.Y;
            }

            Update();
        }

        public void PatrolY(int y1, int y2) //vertical patroling
        {

            if (PatrolingUp == true)        //patrol up
                NPC.Position.Y += -NPC.Move.Y;

            if (PatrolingDown == true)      //patrol down
                NPC.Position.Y += NPC.Move.Y;

            if (NPC.Position.Y <= y1)            //top point
            {
                PatrolingLeft = false;
                PatrolingRight = true;
            }

            if (NPC.Position.Y >= y2)            //button point
            {
                PatrolingLeft = true;
                PatrolingRight = false;
            }

            Update();
        }

        public void Chase()
        {
            if (Game1.Player.Getbounds().Intersects(NPC.SightBounds())) //check if player enter light
            {
                chase = true;
            }
        }
        
        public void Update()
        {
            //blinking stuff
            if (chase && ChaseTimer <= 1) 
            {
                Alarm = true;
            }
            else
            {
                Alarm = false;
            }

            //PATROLING STUFF
            if (time <= 0) //if patrol should move
            {
                NPC.ResetMove();
            }
            else //patrol paused
            {
                NPC.Move.X = 0;
                NPC.Move.Y = 0;
            }

            if (time > 0) //if paused times untill moving 
            {
                time -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            }

            //CHASING STUFF
            Chase();

            if (chase)
            {
                pathFinder.PathCollider();

                NPC.Move = pathFinder.SetSpeed();

                PatrolingLeft = false;
                PatrolingRight = false;
                PatrolingUp = false;
                PatrolingDown = false;

                ChaseTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (NPC.Position.X < Game1.Player.Position.X)//x axel comparision
                {
                    TempLoc.X += NPC.Move.X;  //chase right 
                    NPC.SightRight = true;
                    NPC.SightLeft = false;
                }
                else
                {
                    TempLoc.X += NPC.Move.X; // chase left
                    NPC.SightRight = false;
                    NPC.SightLeft = true;
                }

                if (NPC.Position.Y < Game1.Player.Position.Y)//x axel comparision
                {
                    TempLoc.Y += NPC.Move.Y;  //chase Up
                    NPC.SightUp = false;
                    NPC.SightDown = true;
                    NPC.SightLeft = false;
                    NPC.SightRight = false;
                }
                else
                {
                    TempLoc.Y += NPC.Move.Y; // chase Down
                    NPC.SightUp = true;
                    NPC.SightDown = false;
                    NPC.SightLeft = false;
                    NPC.SightRight = false;
                }

                if (ChaseTimer >= 30 || NPC.Getbounds().Intersects(Game1.Player.Getbounds())) //ends the chase
                {
                    chase = false;
                    ChaseTimer = 0;
                }

                NPC.Sensor.X = (int)TempLoc.X;
                NPC.Sensor.Y = (int)TempLoc.Y;

                for (int i = 0; i < Game1.Lvl1.Solids.Count; i++) //check for base map collision
                {
                    if (Game1.Lvl1.Solids[i].Intersects(NPC.Sensor))
                    {
                        SensorCheck = false;
                        break;
                    }
                }

                if (SensorCheck)
                {
                    NPC.Position.X = TempLoc.X;
                    NPC.Position.Y = TempLoc.Y;
                }
            }
        }
    }
}